package pendotech.com.pendootg;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by dh on 2021/11/11.
 */

public class MyCanvasView extends View {


    private Canvas mCanvas;
    private Bitmap mBitmap;
    private Paint pathPaint;
    private Path mPath;

    /**
     * 绘制bitmap的paint
     */
    private Paint mPaint;

    /**
     * 上一记录的次x
     */
    private float mX;
    /**
     * 上一次记录的y
     */
    private float mY;
    /**
     * 最小的移动宽度
     */
    private final int TOUCH_TOLERANCE = 4;

    public MyCanvasView(Context context) {
        this(context,null);
    }

    public MyCanvasView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyCanvasView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        pathPaint = new Paint();
        pathPaint.setAntiAlias(true);
        pathPaint.setColor(Color.BLACK);
        pathPaint.setStrokeWidth(5);
        pathPaint.setStrokeCap(Paint.Cap.ROUND);
        pathPaint.setStyle(Paint.Style.STROKE);
        mPath = new Path();

        mPaint = new Paint(Paint.DITHER_FLAG);



    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, 0, 0, mPaint);
        canvas.drawPath(mPath, pathPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        int action = event.getAction();
        Log.i("finger",x+"-----"+y);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                fingerDown(x, y);
                break;
            case MotionEvent.ACTION_MOVE:
                fingerMove(x, y);
                break;
            case MotionEvent.ACTION_UP:
                fingerUp();
                break;
            default:
        }
        return true;
    }

    /**
     * 手指按下
     *
     * @param x
     * @param y
     */
    private void fingerDown(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    /**
     * 手指移动
     *
     * @param x
     * @param y
     */
    private void fingerMove(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            //贝塞尔曲线
            mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
            mX = x;
            mY = y;
        }
        invalidate();
    }

    /**
     * 手指抬起
     */
    private void fingerUp() {
        mCanvas.drawPath(mPath, pathPaint);
        mPath.reset();
    }

    /**
     * 清空画布
     */
    public void clear() {
        mCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
        invalidate();
    }

    private int lastPointP=0;
    private int currentPointP;


    public void hypenRealtimEvent(int x,int y,int p){
        currentPointP = p;

        if (lastPointP == 0 && currentPointP != 0) {   // down
            //mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;

        }else if (lastPointP!=0&&currentPointP!=0){   // move

            penWidthLast=lastPointP/1000;
            lastPointP=currentPointP;

            if (Math.abs(currentPointP/1000 - penWidthLast) > 1){
                if (currentPointP/1000>penWidthLast){
                    pathPaint.setStrokeWidth(penWidthLast+1);
                }else {
                    pathPaint.setStrokeWidth(penWidthLast-1);
                }

            }else {
                pathPaint.setStrokeWidth(penWidthLast);
            }

            //贝塞尔曲线
            mPath.quadTo(x, y, (x + mX) / 2, (y + mY) / 2);
            //mPath.lineTo(x,y);
            //mPath.close();
            mCanvas.drawPath(mPath, pathPaint);
            invalidate();
            mPath.reset();
            mPath.moveTo((x + mX) / 2, (y + mY) / 2);

            mX = x;
            mY = y;

        }else if (lastPointP!=0&&currentPointP==0){  //up
            //mPath.lineTo(x,y);
            //mCanvas.drawPath(mPath, pathPaint);
            //invalidate();
            mPath.reset();
        }
        lastPointP=currentPointP;
    }




   private int penWidthLast;


    public Bitmap getMyBitmap(){
        Bitmap newBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_4444);
        Canvas canvas = new Canvas(newBitmap);
        canvas.drawColor(getResources().getColor(R.color.colorAccent));
        Paint paint = new Paint();
        canvas.drawBitmap(mBitmap, 0, 0, paint);
        return newBitmap;
    }



}
